<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>悬浮应用</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            overflow: hidden;
            user-select: none;
        }

        .floating-container {
            width: 100%;
            height: 100vh;
            display: flex;
            flex-direction: column;
        }

        .title-bar {
            background: rgba(0, 0, 0, 0.2);
            padding: 8px 12px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            cursor: move;
            -webkit-app-region: drag;
        }

        .title {
            font-size: 14px;
            font-weight: bold;
        }

        .control-buttons {
            display: flex;
            gap: 8px;
            -webkit-app-region: no-drag;
        }

        .btn {
            background: rgba(255, 255, 255, 0.2);
            border: none;
            color: white;
            width: 20px;
            height: 20px;
            border-radius: 3px;
            cursor: pointer;
            font-size: 12px;
            transition: background 0.2s;
        }

        .btn:hover {
            background: rgba(255, 255, 255, 0.3);
        }

        .btn.close:hover {
            background: #ff4757;
        }

        .content {
            flex: 1;
            padding: 16px 16px 10px 16px;  /* 底部padding改为10px */
            display: flex;
            flex-direction: column;
            gap: 12px;
            overflow: visible;
        }

        .status-section, .config-section, .last-data {
            background: rgba(255, 255, 255, 0.1);
            border-radius: 8px;
            padding: 12px;
            backdrop-filter: blur(10px);
        }

        .status-title, .config-title, .last-data-title {
            font-size: 12px;
            font-weight: bold;
            margin-bottom: 8px;
            color: #f1f2f6;
        }

        .status-grid {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 8px;
        }

        .status-item {
            display: flex;
            align-items: center;
            font-size: 11px;
            gap: 4px;  /* 进一步减少间距 */
        }

        .status-item span {
            white-space: nowrap;  /* 防止文字换行 */
        }

        .status-indicator {
            width: 8px;
            height: 8px;
            border-radius: 50%;
            background: #ff4757;
            transition: background 0.3s;
            flex-shrink: 0;  /* 防止被压缩 */
            margin-left: 2px;  /* 微调左边距 */
        }

        .status-indicator.connected {
            background: #2ed573;
        }

        .config-grid {
            display: flex;
            flex-direction: column;
            gap: 6px;
        }

        .config-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            font-size: 10px;
        }

        .config-label {
            color: #f1f2f6;
            margin-right: 8px;
        }

        .config-value, .config-value-multiline {
            color: #ddd;
            max-width: 180px;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            font-size: 10px;
            line-height: 1.2;
            user-select: text;
            cursor: pointer;
        }
        .config-value-multiline {
            white-space: pre-wrap;
            word-break: break-all;
            margin: 0;
            padding: 0;
        }

        .action-buttons {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 8px;
            margin-top: auto;  /* 推到底部 */
            padding-top: 8px;
        }

        .action-btn {
            padding: 8px 12px;
            border: none;
            border-radius: 6px;
            font-size: 11px;
            font-weight: bold;
            cursor: pointer;
            transition: all 0.2s;
        }

        .action-btn.primary {
            background: #2ed573;
            color: white;
        }

        .action-btn.primary:hover {
            background: #26d0ce;
        }

        .action-btn.danger {
            background: #ff4757;
            color: white;
        }

        .action-btn.danger:hover {
            background: #ff3742;
        }

        .action-btn.config {
            grid-column: 1 / -1;
            background: #3742fa;
            color: white;
        }

        .action-btn.config:hover {
            background: #2f3542;
        }

        .last-data {
            margin-top: 8px;
            height: 120px;  /* 增加高度以容纳5行 */
            overflow: hidden;
        }

        .last-data-content {
            font-size: 10px;
            color: #ddd;
            height: 80px;  /* 增加高度以容纳5行 */
            overflow-y: auto;
            word-break: break-all;
            line-height: 1.4;
            padding: 4px;
        }

        .last-data-content:empty::before {
            content: "暂无数据";
            color: #aaa;
            font-style: italic;
        }

        .timestamp {
            font-size: 9px;
            color: #aaa;
            margin-top: 4px;
            height: 20px;
        }

        .notification {
            position: fixed;
            top: 20px;
            right: 20px;
            background: #2ed573;
            color: white;
            padding: 12px 16px;
            border-radius: 8px;
            font-size: 12px;
            z-index: 1000;
            transform: translateX(400px);
            transition: transform 0.3s;
        }

        .notification.show {
            transform: translateX(0);
        }

        .no-data {
            color: #aaa;
            font-style: italic;
        }

        /* 悬浮球模式样式 */
        body.floating-ball {
            width: 60px !important;
            height: 60px !important;
            border-radius: 50% !important;
            overflow: hidden !important;
            background: #764ba2 !important;
            transition: all 0.3s;
        }
        .floating-ball-content {
            width: 60px;
            height: 60px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 50%;
            background: transparent;
            -webkit-app-region: drag;   /* 让悬浮球可拖动 */
            cursor: move;
        }
        .floating-ball-icon {
            width: 36px;
            height: 36px;
            border-radius: 50%;
            background: rgba(255,255,255,0.8);
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 24px;
            color: #764ba2;
            box-shadow: 0 2px 8px rgba(0,0,0,0.12);
            pointer-events: none; /* 图标本身不拦截拖动 */
        }
        body.floating-ball .floating-container,
        body.floating-ball .content,
        body.floating-ball .title-bar,
        body.floating-ball .notification {
            display: none !important;
        }
        body.floating-ball .floating-ball-content {
            display: flex !important;
        }
    </style>
</head>
<body>
    <div class="floating-container">
        <div class="title-bar">
            <div class="title">小助手</div>
            <div class="control-buttons">
                <button class="btn" onclick="minimizeWindow()" title="最小化到托盘">—</button>
                <button class="btn close" onclick="closeWindow()" title="隐藏到托盘">×</button>
            </div>
        </div>

        <div class="content">
            <!-- 状态区 -->
            <div class="status-section">
                <div class="status-title">连接状态</div>
                <div class="status-grid">
                    <div class="status-item" id="api-status-row">
                        <span>API服务器:</span>
                        <div class="status-indicator" id="api-status"></div>
                    </div>
                    <div class="status-item" id="ws-status-row">
                        <span>WebSocket:</span>
                        <div class="status-indicator" id="ws-status"></div>
                    </div>
                </div>
            </div>

            <!-- 配置区 -->
            <div class="config-section">
                <div class="config-title">当前配置</div>
                <div class="config-grid">
                    <div class="config-item" id="api-config-item">
                        <span class="config-label">API地址:</span>
                        <span class="config-value" id="api-url"></span>
                    </div>
                    <div class="config-item" id="ws-config-item">
                        <span class="config-label">WS地址:</span>
                        <span class="config-value" id="ws-url"></span>
                    </div>
                    <div class="config-item" id="target-config-item">
                        <span class="config-label">目标网址:</span>
                        <div class="config-value config-value-multiline" id="target-url"></div>
                    </div>
                </div>
            </div>

            <div class="last-data" id="last-data-section">
                <div class="last-data-title">最新数据</div>
                <div class="last-data-content" id="last-data-content">
                    <span class="no-data">暂无数据</span>
                </div>
                <div class="timestamp" id="last-data-time"></div>
            </div>

            <div class="action-buttons">
                <button class="action-btn primary" onclick="startListening()">开始</button>
                <button class="action-btn danger" onclick="stopListening()">停止</button>
                <button class="action-btn config" onclick="showConfig()">配置设置</button>
            </div>
        </div>
    </div>

    <div class="notification" id="notification"></div>
    <div class="floating-ball-content" style="display:none;">
        <div class="floating-ball-icon">🤖</div>
    </div>

    <script>
        const { ipcRenderer } = require('electron');

        let isListening = false;

        // 动态渲染配置和状态
        let lastConfig = null;
        let wsConnected = false;
        let apiActive = false;

        function renderConfig(config) {
            lastConfig = config;
            // 动态显示/隐藏
            document.getElementById('api-status-row').style.display = config.useApi ? 'flex' : 'none';
            document.getElementById('api-config-item').style.display = config.useApi ? '' : 'none';
            document.getElementById('ws-status-row').style.display = config.useWebSocket ? 'flex' : 'none';
            document.getElementById('ws-config-item').style.display = config.useWebSocket ? '' : 'none';
            document.getElementById('target-config-item').style.display = (config.useApi || config.useWebSocket) ? '' : 'none';

            // 填充文本
            document.getElementById('api-url').textContent = config.apiUrl || '';
            document.getElementById('ws-url').textContent = config.wsUrl || '';
            document.getElementById('target-url').textContent = config.targetUrl || '';

            // 状态点刷新
            updateStatusDots();
        }

        function updateStatusDots() {
            // API
            const apiStatus = document.getElementById('api-status');
            if (lastConfig && lastConfig.useApi) {
                apiStatus.classList.toggle('connected', apiActive);
                apiStatus.style.visibility = '';
            } else {
                apiStatus.style.visibility = 'hidden';
            }
            // WS
            const wsStatus = document.getElementById('ws-status');
            if (lastConfig && lastConfig.useWebSocket) {
                wsStatus.classList.toggle('connected', wsConnected);
                wsStatus.style.visibility = '';
            } else {
                wsStatus.style.visibility = 'hidden';
            }
        }

        // 初始化
        document.addEventListener('DOMContentLoaded', () => {
            console.log('悬浮窗口DOM加载完成');
            loadConfig();
            updateStatus();
            
            // 通知主进程悬浮窗口已准备就绪
            ipcRenderer.send('floating-window-ready');
        });

        // 加载配置
        function loadConfig() {
            ipcRenderer.send('get-config');
        }

        // 更新状态显示
        function updateStatus() {
            ipcRenderer.send('get-config');
        }

        // 显示通知
        function showNotification(message, duration = 3000) {
            const notification = document.getElementById('notification');
            notification.textContent = message;
            notification.classList.add('show');
            
            setTimeout(() => {
                notification.classList.remove('show');
            }, duration);
        }

        // 窗口控制
        function minimizeWindow() {
            ipcRenderer.send('minimize-floating');
        }

        function closeWindow() {
            ipcRenderer.send('close-floating');
        }

        // 监听控制
        function startListening() {
            ipcRenderer.send('start-listening');
            isListening = true;
            showNotification('开始监听...');
        }

        function stopListening() {
            ipcRenderer.send('stop-listening');
            isListening = false;
            showNotification('停止监听');
        }

        function showConfig() {
            ipcRenderer.send('show-config');
        }

        function copyToClipboard(text) {
            if (navigator.clipboard) {
                navigator.clipboard.writeText(text).then(() => {
                    showNotification('已复制到剪贴板');
                });
            } else {
                // 兼容旧浏览器
                const textarea = document.createElement('textarea');
                textarea.value = text;
                document.body.appendChild(textarea);
                textarea.select();
                document.execCommand('copy');
                document.body.removeChild(textarea);
                showNotification('已复制到剪贴板');
            }
        }

        function addCopyListener(id) {
            const el = document.getElementById(id);
            if (el) {
                el.ondblclick = function(e) {
                    e.stopPropagation();
                    copyToClipboard(el.textContent || el.value || '');
                };
            }
        }

        // IPC事件监听
        ipcRenderer.on('config-data', (event, config) => {
            renderConfig(config);
        });

        ipcRenderer.on('ws-status', (event, { connected }) => {
            wsConnected = connected;
            updateStatusDots();
        });

        ipcRenderer.on('api-status', (event, { active }) => {
            apiActive = active;
            updateStatusDots();
        });

        ipcRenderer.on('data-received', (event, data) => {
            console.log('悬浮窗口收到数据:', data);
            
            const content = document.getElementById('last-data-content');
            const time = document.getElementById('last-data-time');
            
            console.log('更新最新数据显示');
            console.log('数据内容:', data.data);
            console.log('时间戳:', data.timestamp);
            
            // 更新内容
            content.innerHTML = `<span>${data.data}</span>`;
            time.textContent = data.timestamp;
            
            console.log('最新数据已更新');
            showNotification('收到新数据！');
        });

        // 自动开始监听
        setTimeout(() => {
            if (!isListening) {
                startListening();
            }
        }, 1000);

        document.addEventListener('DOMContentLoaded', () => {
            addCopyListener('api-url');
            addCopyListener('ws-url');
            addCopyListener('target-url');
        });

        // 悬浮球/悬浮窗切换
        ipcRenderer.on('switch-to-ball', () => {
            document.body.classList.add('floating-ball');
            document.querySelector('.floating-ball-content').style.display = 'flex';
        });
        ipcRenderer.on('switch-to-window', () => {
            document.body.classList.remove('floating-ball');
            document.querySelector('.floating-ball-content').style.display = 'none';
        });
        // 阻止悬浮球区域双击冒泡，改为还原悬浮窗
        document.addEventListener('DOMContentLoaded', () => {
            const ball = document.querySelector('.floating-ball-content');
            if (ball) {
                ball.addEventListener('dblclick', e => {
                    e.stopImmediatePropagation();
                    e.stopPropagation();
                    e.preventDefault();
                    // 发送还原请求
                    if (typeof require === 'function') {
                        const { ipcRenderer } = require('electron');
                        ipcRenderer.send('restore-floating-window');
                    }
                }, true); // 捕获阶段
            }
        });
    </script>
</body>
</html> 